package net.arvin.mobilebanksystem.notused;

import java.util.ArrayList;
import java.util.List;

import net.arvin.dao.ViewPagerAdapter;
import net.arvin.mobilebanksystem.R;
import net.arvin.mobilebanksystem.fragment.HelperFragment;
import net.arvin.mobilebanksystem.fragment.LifeFragment;
import net.arvin.mobilebanksystem.fragment.MainPageFragment;
import net.arvin.view.ArvinViewPager;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

@SuppressWarnings("deprecation")
public class MobileBankActivity extends FragmentActivity implements
		OnClickListener, OnPageChangeListener {
	public ArvinViewPager mPager;
	private RelativeLayout mainPage;
	private RelativeLayout life;
	private RelativeLayout helper;
	private RelativeLayout myInfo;
	private LinearLayout guidBar;

	private List<View> mViews;
	
	private LocalActivityManager manager = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_mobilebank);
		manager = new LocalActivityManager(this , true);
        manager.dispatchCreate(savedInstanceState);
		init();
	}

	private void init() {
		initView();
		initFragment();
		initListener();
		initViewPage();
	}

	private void initViewPage() {
		mPager.setAdapter(new ViewPagerAdapter(mViews));
		mPager.setOnPageChangeListener(this);
	}

	private void initListener() {
		mainPage.setOnClickListener(this);
		life.setOnClickListener(this);
		helper.setOnClickListener(this);
		myInfo.setOnClickListener(this);
	}

	private void initFragment() {
		mViews = new ArrayList<View>();
		mViews.add(getView("",new Intent(this,MainPageFragment.class)));
		mViews.add(getView("",new Intent(this,LifeFragment.class)));
		mViews.add(getView("",new Intent(this,HelperFragment.class)));
//		mViews.add(getView("",new Intent(this,MineActivity.class)));
	}
	
	private View getView(String id, Intent intent) {
        return manager.startActivity(id, intent).getDecorView();
    }
	
	private void initView() {
		mPager = (ArvinViewPager) findViewById(R.id.contentPager);
		mainPage = (RelativeLayout) findViewById(R.id.mainPage);
		life = (RelativeLayout) findViewById(R.id.life);
		helper = (RelativeLayout) findViewById(R.id.helper);
		myInfo = (RelativeLayout) findViewById(R.id.myInfo);
		guidBar = (LinearLayout) findViewById(R.id.guidBar);
		mainPage.setSelected(true);
	}

	public void hidenGuidBar() {
		guidBar.setVisibility(View.GONE);
	}

	public void showGuidBar() {
		guidBar.setVisibility(View.VISIBLE);
	}

	private void clearAllSelect() {
		mainPage.setSelected(false);
		life.setSelected(false);
		helper.setSelected(false);
		myInfo.setSelected(false);
	}

	@Override
	public void onClick(View v) {
		clearAllSelect();
		switch (v.getId()) {
		case R.id.mainPage:
			mainPage.setSelected(true);
			mPager.setCurrentItem(0, false);
			break;
		case R.id.life:
			life.setSelected(true);
			mPager.setCurrentItem(1, false);
			break;
		case R.id.helper:
			helper.setSelected(true);
			mPager.setCurrentItem(2, false);
			break;
		case R.id.myInfo:
			myInfo.setSelected(true);
			mPager.setCurrentItem(3, false);
			break;
		}
	}

	@Override
	public void onPageScrollStateChanged(int state) {
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}

	@Override
	public void onPageSelected(int position) {
		clearAllSelect();
		switch (position) {
		case 0:
			mainPage.setSelected(true);
			break;
		case 1:
			life.setSelected(true);
			break;
		case 2:
			helper.setSelected(true);
			break;
		case 3:
			myInfo.setSelected(true);
			break;
		}
	}
}